#include "connectedtitlewin.h"

#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QFont>
#include "generatetools.h"
#include "highlight-effect.h"
#include "systemfontsize.h"

ConnectedTitleWin::ConnectedTitleWin()
{
    m_stateLab = new QLabel(this);
    m_iconLab = new QLabel(this);
    m_mobileNameLab = new QLabel(this);
    m_showMobileWinBtn = new QPushButton(this);
    m_disconnectBtn = new QPushButton(this);
    m_splitLine = new SplitLine(784);

    //初始时设置字号
    SystemFontSize::getInstance()->getSystemFontSize();
    QFont font16;
    font16 = SystemFontSize::getInstance()->getFontSize16px();
    font16.setBold(true);
    m_stateLab->setFont(font16);

    m_iconLab->setFixedSize(20, 20);

    m_stateLab->setText(tr("Connected"));
    m_showMobileWinBtn->setText(tr("Mobile Window"));
    m_disconnectBtn->setText(tr("Disconnect"));
    m_showMobileWinBtn->setFocusPolicy(Qt::NoFocus);
    m_showMobileWinBtn->setProperty("isImportant", true);
    m_disconnectBtn->setFocusPolicy(Qt::NoFocus);

    connect(m_disconnectBtn, &QPushButton::clicked, this, &ConnectedTitleWin::onClicked);
    connect(m_showMobileWinBtn, &QPushButton::clicked, this, &ConnectedTitleWin::onClicked);

    QHBoxLayout *hLayout = new QHBoxLayout;
    hLayout->addWidget(m_stateLab);
    hLayout->addSpacing(10);
    hLayout->addWidget(m_iconLab);
    hLayout->addSpacing(4);
    hLayout->addWidget(m_mobileNameLab);
    hLayout->addSpacing(28);
    hLayout->addWidget(m_showMobileWinBtn);
    hLayout->addStretch();
    hLayout->addWidget(m_disconnectBtn);
    hLayout->setMargin(0);

    QVBoxLayout *vLayout = new QVBoxLayout;
    vLayout->addLayout(hLayout);
    vLayout->addSpacing(10);
    vLayout->addWidget(m_splitLine);
    vLayout->addStretch();
    vLayout->setContentsMargins(68, 20, 68, 0);

    setLayout(vLayout);

    connect(SystemFontSize::getInstance(), &SystemFontSize::signalChangeFontSize, this, [=]() {
        QFont font16;
        font16 = SystemFontSize::getInstance()->getFontSize16px();
        font16.setBold(true);
        m_stateLab->setFont(font16);
    });
}

ConnectedTitleWin::~ConnectedTitleWin()
{
    if (m_splitLine != nullptr) {
        delete m_splitLine;
        m_splitLine = nullptr;
    }
}

void ConnectedTitleWin::setLightUI()
{
    m_iconLab->setPixmap(QIcon(":/icon/mobileLightIcon.png").pixmap(20, 20));
}

void ConnectedTitleWin::setDarkUI()
{
    m_iconLab->setPixmap(QIcon(":/icon/mobileDarkIcon.png").pixmap(20, 20));
}


void ConnectedTitleWin::setMobileName(QString mobileName)
{
    m_mobileNameLab->setText(mobileName);
}

void ConnectedTitleWin::setShowMobileWinBtn(bool isShow)
{
    if (isShow) {
        m_showMobileWinBtn->hide();
    } else {
        m_showMobileWinBtn->show();
    }
}

void ConnectedTitleWin::onClicked()
{
    QPushButton *btn = qobject_cast<QPushButton *>(sender());
    QString str = btn->text();

    if (tr("Disconnect") == str) {
        Q_EMIT sigDisconnectBtnClicked();
        m_showMobileWinBtn->show();
        // 断开连接埋点
        GenerateTools::buriedPoint(kabase::BuriedPoint::PT::KylinMobileAssistantDisconnect);
    } else if (tr("Mobile Window") == str) {
        Q_EMIT sigSetMobileWindow();
        // 手机投屏埋点
        GenerateTools::buriedPoint(kabase::BuriedPoint::PT::KylinMobileAssistantMobileScreen);
    }
}
